/*
 * *******************************************************************
 *   @项目名称: BHex Android
 *   @文件名称: MaketFuturesSwitchProvider.java
 *   @Date: 19-7-22 下午8:04
 *   @Author: ppzhao
 *   @Description:
 *   @Copyright（C）: 2019 BlueHelix Inc.   All rights reserved.
 *   注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的.
 *  *******************************************************************
 */

package io.bhex.app.market.adapter.provider;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.MotionEvent;
import android.view.View;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.provider.BaseItemProvider;

import io.bhex.app.R;
import io.bhex.app.market.adapter.MarketListMultiAdapter;
import io.bhex.app.market.event.ItemClickStatusListener;
import io.bhex.app.utils.KlineUtils;
import io.bhex.app.utils.NumberUtils;
import io.bhex.app.utils.SkinColorUtil;
import io.bhex.baselib.utils.DebugLog;
import io.bhex.sdk.quote.bean.CoinPairBean;
import io.bhex.sdk.quote.bean.FuturensBaseToken;
import io.bhex.sdk.quote.bean.TickerBean;

public class MaketFuturesSwitchProvider extends BaseItemProvider<CoinPairBean, BaseViewHolder> {

    private static final String TGA = "MaketFuturesSwitchProvider";
    private String mSelectedSymbolId;
    private ItemClickStatusListener mItemClickStatusListener;

    public MaketFuturesSwitchProvider(String selectedSymbolId, ItemClickStatusListener itemClickStatusListener) {
        mSelectedSymbolId = selectedSymbolId;
        mItemClickStatusListener = itemClickStatusListener;
    }

    @Override
    public int viewType() {
        return MarketListMultiAdapter.ITEM_TYPE_FUTURES_SWITCH;
    }

    @Override
    public int layout() {
        return R.layout.item_market_list_select_layout;
    }

    @Override
    public void convert(BaseViewHolder baseViewHolder, final CoinPairBean itemModel, int position) {
        final Context myContext = mContext;
        baseViewHolder.setGone(R.id.item_range_ratio, false);//隐藏数量
        FuturensBaseToken baseTokenFutures = itemModel.baseTokenFutures;
        if (baseTokenFutures != null) {
            TickerBean tickerBean = itemModel.getTicker();
            String quoteTokenId = itemModel.getQuoteTokenId();
            String symbolStr = itemModel.getSymbolName();
//            SpannableStringBuilder builder = new SpannableStringBuilder(symbolStr);
//            ForegroundColorSpan quoteColor = new ForegroundColorSpan(getResources().getColor(R.color.font_color2));
//            builder.setSpan(quoteColor, symbolStr.indexOf("/"), symbolStr.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            int baseDigit = NumberUtils.calNumerCount(myContext, itemModel.getBasePrecision());
            int pricDigit = NumberUtils.calNumerCount(myContext, itemModel.getMinPricePrecision());

            baseViewHolder.setText(R.id.item_coinpair, symbolStr);

            baseViewHolder.getView(R.id.select_item_view).setSelected(mSelectedSymbolId.equals(itemModel.getSymbolId()));

            baseViewHolder.setGone(R.id.option_left_text, false);
            if (tickerBean != null) {

                baseViewHolder.setText(R.id.item_price1, NumberUtils.roundFormatDown(tickerBean.getC(), pricDigit));

                float riseFallAmount = KlineUtils.calRiseFallAmountFloat(tickerBean.getC(), tickerBean.getO());
                if (riseFallAmount > 0) {
                    baseViewHolder.setTextColor(R.id.item_price1, ContextCompat.getColor(myContext, R.color.green));
                } else if (riseFallAmount < 0) {
                    baseViewHolder.setTextColor(R.id.item_price1, ContextCompat.getColor(myContext, R.color.red));
                } else {//涨跌幅0默认为涨 显示为绿色
                    baseViewHolder.setTextColor(R.id.item_price1, SkinColorUtil.getDark50(myContext));
                }
            } else {
                baseViewHolder.setText(R.id.item_price1, myContext.getString(R.string.string_placeholder));
            }

            baseViewHolder.getView(R.id.itemView).setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()){
                        case MotionEvent.ACTION_DOWN:
                            if (mItemClickStatusListener != null) {
                                mItemClickStatusListener.onItemStatusListener(true);
                            }
                            DebugLog.e(TGA,"RecyclerView_ITEM_ACTION_DOWN");
                            break;

                        case MotionEvent.ACTION_MOVE:
                            DebugLog.e(TGA,"RecyclerView_ITEM_ACTION_MOVE");

                            break;

                        case MotionEvent.ACTION_UP:
                            DebugLog.e(TGA,"RecyclerView_ITEM_ACTION_UP");
                            if (mItemClickStatusListener != null) {
                                mItemClickStatusListener.onItemStatusListener(false);
                            }
                            break;

                        case MotionEvent.ACTION_CANCEL:
                            DebugLog.e(TGA,"RecyclerView_ITEM_ACTION_CANCEL");
                            if (mItemClickStatusListener != null) {
                                mItemClickStatusListener.onItemStatusListener(false);
                            }
                            break;
                    }
                    return false;
                }
            });
            
            baseViewHolder.addOnClickListener(R.id.itemView);
        }
    }

    @Override
    public void onClick(BaseViewHolder helper, CoinPairBean data, int position) {
        super.onClick(helper, data, position);
//        IntentUtils.goKline(mContext, data);
    }
}
